package com.redsoil.house.ui.news.fragment;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.ListHolder;
import com.orhanobut.dialogplus.OnClickListener;
import com.orhanobut.dialogplus.OnItemClickListener;
import com.orhanobut.dialogplus.ViewHolder;
import com.redsoil.house.R;
import com.redsoil.house.base.RootFragment;
import com.redsoil.house.model.bean.DictListVO;
import com.redsoil.house.model.bean.NewsVO;
import com.redsoil.house.model.bean.TestVO;
import com.redsoil.house.model.http.api.ApiConstants;
import com.redsoil.house.ui.news.activity.NewsInfoActivity;
import com.redsoil.house.ui.news.adapter.NewsAdapter;
import com.redsoil.house.ui.news.adapter.NewsTitleAdapter;
import com.redsoil.house.ui.news.contract.NewsListContract;
import com.redsoil.house.ui.news.presenter.NewsListPresenter;
import com.redsoil.house.widget.actionbar.NewsTopBar;
import com.redsoil.house.widget.actionbar.NewsTopBarImp;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 咨询列表
 */
public class NewsListFragment extends RootFragment<NewsListPresenter> implements NewsListContract.View ,NewsTopBarImp{

    @BindView(R.id.rv_list)
    RecyclerView rv_list;

    @BindView(R.id.news_topbar)
    NewsTopBar news_topbar;

    private NewsAdapter newsAdapter;

    private List<DictListVO.DataListBean> beans;

    private DialogPlus filterDialog;

    private NewsTitleAdapter newsTitleAdapter;

    public static NewsListFragment newInstance() {
        Bundle args = new Bundle();

        NewsListFragment fragment = new NewsListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initEventAndData() {
        super.initEventAndData();

        stateLoading();

        news_topbar.setNewsTopBarImp(this);

        mPresenter.getDicList();
    }

    @Override
    public void getDicListSuccess(DictListVO dictListVO) {
        beans = dictListVO.getDataList();

        news_topbar.setData(beans.get(0).getValue());

        mPresenter.postNews(beans.get(0).getOrder());
    }

    @Override
    public void responeError(String errorMsg) {
        stateError();
    }

    @Override
    public void postNewsSuccess(NewsVO newsVO) {
        if(newsAdapter == null){
            //添加自定义分割线
            DividerItemDecoration divider = new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL);
            divider.setDrawable(ContextCompat.getDrawable(getContext(),R.drawable.mine_specail_list_divider));
            rv_list.addItemDecoration(divider);

            rv_list.setLayoutManager(new LinearLayoutManager(getContext()));


            newsAdapter = new NewsAdapter(R.layout.news_list_item,newsVO.getDataList());

            newsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    getContext().startActivity(NewsInfoActivity.newInstance(getContext(), ApiConstants.NEWS_URL+newsAdapter.getItem(position).getId()));
                }
            });

            rv_list.setAdapter(newsAdapter);
        }else {
            rv_list.smoothScrollToPosition(0);
            newsAdapter.setNewData(newsVO.getDataList());
        }

        stateMain();
    }

    @Override
    public void middle() {
        if (filterDialog == null) {

            newsTitleAdapter = new NewsTitleAdapter(getContext(),beans);

            filterDialog = DialogPlus.newDialog(getContext())
                    .setGravity(Gravity.BOTTOM)
                    .setContentHolder(new ListHolder())
                    .setAdapter(newsTitleAdapter)
                    .setCancelable(true)//设置不可取消   可以取消
                    .setOnItemClickListener(new OnItemClickListener() {
                        @Override
                        public void onItemClick(DialogPlus dialog, Object item, View view, int position) {
                            DictListVO.DataListBean bean = beans.get(position);
                            if(!news_topbar.getTitleLabel().equals(bean.getValue())){
                                news_topbar.setData(bean.getValue());

                                stateLoading();

                                mPresenter.postNews(bean.getOrder());
                            }
                            dialog.dismiss();
                        }
                    })
                    .create();

        }

        filterDialog.show();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_news_list;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }
}
